package com.atguigu.gulimall.order.config;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;


@Configuration
public class MyMQConfig {

//    //之前为了防止宕机丢失消息，开启了消息手动确认，不再自动确认消息
//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
//
//        System.out.println("收到过期的的订单消息，准备关闭订单"+entity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//
//    }

    //容器中的Binding,Queue.Exchange都会自动创建，前提是RabbitMQ没有
    //创建好一个交换机拖了两个队列
    //死信队列
    @Bean
    public Queue orderDelayQueue(){

        HashMap<String, Object> arguments = new HashMap<>();

        //设置死信的消息，到时间后通过那个路由
        //死信路由
        arguments.put("x-dead-letter-exchange","order-event-exchange");

        //通过那个路由键，到时间后的路由键
        //死信路由键
        arguments.put("x-dead-letter-routing-key","order.release.order");

        //消息的延迟时间
        arguments.put("x-message-ttl",60000);

        //死信队列
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);

        return queue;
    }

    //普通队列
    @Bean
    public Queue orderReleaseOrderQueue(){

        //order.release.order.queue
        Queue queue = new Queue("order.release.order.queue", true, false, false);

        return queue;
    }

    //交换机，创建一个topic类型的交换机
    @Bean
    public Exchange orderEventExchange(){
        TopicExchange exchange = new TopicExchange("order-event-exchange", true, false);
        return exchange;
    }


    //消息发送给交换机，交换机发送给死信队列
    //交换机同死信队列绑定
    @Bean
    public Binding orderCreateOrderBinding(){
        Binding binding = new Binding(
                "order.delay.queue",
                 Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null
        );
        return binding;
    }

    //消息发给交换机，交换机发给普通地队列
    //交换机同普通队列绑定
    @Bean
    public Binding orderReleaseOrderBinding(){

        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);

    }

    /**
     * 订单释放直接和库存释放绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOthe(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
